<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-08-01
 * Time: 上午 9:12
 * 小程序登录功能
 */
namespace app\api\controller;
use think\Controller;

class Login extends Controller{
    public function login(){
        $code = input('param.code');
        if(!$code){
            ajaxReturn(-1,'code不能为空');
        }
        $jg_userid = input('param.jg_userid');
        if(!$jg_userid){
            ajaxReturn(-1,'$jg_userid极光的用户唯一标识不能为空');
        }
        $response = sendCurl("https://api.weixin.qq.com/sns/jscode2session?appid=wxed4bee72b547c290&secret=ee741ac187ed182d55b5f6ee3978c550&js_code=".$code."&grant_type=authorization_code");
        $response = json_decode($response, true);
        if (!empty($response['errcode'])) {
            ajaxReturn(-1,'操作失败!'.$response['errmsg']);
        }
        $openid     = $response['openid'];
        $sessionKey = $response['session_key'];

        //检测当前用户是不是第一次登录
        $member = new Member();
        $is_status = $member->checkMemberFirst($openid);
        if($is_status == true){
            //第一次 注册会员
            $data['opend_id'] = $openid;
            $data['jg_userid'] = $jg_userid;
            $insert_status = $member->addMember($data);
            if(!$insert_status){
                ajaxReturn(-1,'登录失败');
            }else{
                $memberInfo = $member->getMemberInfo($insert_status);
                ajaxReturn(1,'登录成功',$memberInfo);
            }
        }else{
            //第二次 直接获取用户数据
            $memberInfo = $member->getMemberInfoOpendid($openid);
            ajaxReturn(1,'登录成功',$memberInfo);
        }
    }
}